/**
 * 剑指 Offer 57. 和为s的两个数字
 * https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/
 */
const { displayBaseArray } = require('../../libs/public');

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
function twoSum(nums, target) {
  if (nums.length < 2) {
    return [];
  }
  let left = 0;
  let right = nums.length - 1;
  while (left < right) {
    const sum = nums[left] + nums[right];
    if (sum === target) {
      return [nums[left], nums[right]];
    }
    if (sum < target) {
      left += 1;
    } else {
      right -= 1;
    }
  }
  return [];
}

console.log(displayBaseArray(twoSum([2, 7, 11, 15], 9)) === '[2,7]');
console.log(displayBaseArray(twoSum([10, 26, 30, 31, 47, 60], 40)) === '[10,30]');
